import request from '@/utils/request';
import type { BasicMsg, BasicMsgWithData, Order, OrderDetail, CreateOrderRequest } from '@/types';

// 获取所有订单
export const getAllOrders = (): Promise<BasicMsgWithData<Order[]>> => {
  return request({
    url: '/api/orders',
    method: 'get'
  });
};

// 根据ID获取订单
export const getOrderById = (id: number): Promise<BasicMsgWithData<Order>> => {
  return request({
    url: `/api/orders/${id}`,
    method: 'get'
  });
};

// 创建订单
export const createOrder = (data: CreateOrderRequest): Promise<BasicMsg> => {
  return request({
    url: '/api/orders',
    method: 'post',
    data
  });
};

// 更新订单
export const updateOrder = (id: number, data: Partial<Order>): Promise<BasicMsg> => {
  return request({
    url: `/api/orders/${id}`,
    method: 'put',
    data
  });
};

// 删除订单
export const deleteOrder = (id: number): Promise<BasicMsg> => {
  return request({
    url: `/api/orders/${id}`,
    method: 'delete'
  });
};

// 根据用户ID获取订单
export const getOrdersByUserId = (userId: number): Promise<BasicMsgWithData<Order[]>> => {
  return request({
    url: `/api/orders/user/${userId}`,
    method: 'get'
  });
};

// 根据订单号查询订单
export const getOrderByOrderNo = (orderNo: string): Promise<BasicMsgWithData<Order>> => {
  return request({
    url: `/api/orders/orderNo/${orderNo}`,
    method: 'get'
  });
};

// 根据订单状态查询订单
export const getOrdersByStatus = (status: string): Promise<BasicMsgWithData<Order[]>> => {
  return request({
    url: `/api/orders/status/${status}`,
    method: 'get'
  });
};

// 取消订单
export const cancelOrder = (id: number): Promise<BasicMsg> => {
  return request({
    url: `/api/orders/${id}/cancel`,
    method: 'put'
  });
};

// 支付订单
export const payOrder = (id: number, paymentMethod: string): Promise<BasicMsg> => {
  return request({
    url: `/api/orders/${id}/pay`,
    method: 'put',
    data: { paymentMethod }
  });
};

// 完成订单
export const completeOrder = (id: number): Promise<BasicMsg> => {
  // 通过更新订单状态为已完成
  return request({
    url: `/api/orders/${id}`,
    method: 'put',
    data: { orderStatus: '2' }
  });
};

// 创建订单详情
export const createOrderDetail = (orderId: number): Promise<BasicMsg> => {
  return request({
    url: '/api/order-details',
    method: 'post',
    data: { orderId }
  });
};

// 根据订单ID获取订单详情
export const getOrderDetailsByOrderId = (orderId: number): Promise<BasicMsgWithData<OrderDetail[]>> => {
  return request({
    url: `/api/order-details/order/${orderId}`,
    method: 'get'
  });
};

